﻿using iTextSharp.text.pdf;
using iTextSharp.text.pdf.parser;
using Microsoft.Office.Core;
using Microsoft.Office.Interop.Word;
using Reader.Libs.PDF.Helper;
using Reader.Libs.PRC;
using System;
using System.IO;
using System.Text;

namespace Reader.Libs.CONVERTER
{
    public class MobiConverter
    {
        public void ConvertToTxt(string SourcePath, string FileToSave)
        {
            if (File.Exists(FileToSave))
            {
                File.Delete(FileToSave);
            }
            try
            {
                if (!System.IO.Directory.Exists("C://tmp"))
                    System.IO.Directory.CreateDirectory("C://tmp");
                else
                {
                    foreach (FileInfo f in (new DirectoryInfo("C://tmp")).GetFiles())
                        f.Delete();
                    foreach (DirectoryInfo dir in (new DirectoryInfo("C://tmp")).GetDirectories())
                    {
                        dir.Delete(true);
                    }
                }

                MobiDoc md = new MobiDoc();
                md.loadContent(SourcePath);

                System.IO.FileStream fs = new System.IO.FileStream("C://tmp//tmp.html", System.IO.FileMode.Create, System.IO.FileAccess.Write);
                StreamWriter sw = new StreamWriter(fs);
                sw.Write(md.BookContent);
                sw.Close();
                fs.Close();

                (new HtmlConverter()).ConvertToTxt("C://tmp//tmp.html", FileToSave);
            }
            catch { throw; } 
        }

        public void ConvertToDoc(string SourcePath, string FileToSave)
        {
            if (File.Exists(FileToSave))
            {
                File.Delete(FileToSave);
            }
            try
            {
                if (!System.IO.Directory.Exists("C://tmp"))
                    System.IO.Directory.CreateDirectory("C://tmp");
                else
                {
                    foreach (FileInfo f in (new DirectoryInfo("C://tmp")).GetFiles())
                        f.Delete();
                    foreach (DirectoryInfo dir in (new DirectoryInfo("C://tmp")).GetDirectories())
                    {
                        dir.Delete(true);
                    }
                }

                MobiDoc md = new MobiDoc();
                md.loadContent(SourcePath);

                System.IO.FileStream fs = new System.IO.FileStream("C://tmp//tmp.html", System.IO.FileMode.Create, System.IO.FileAccess.Write);
                StreamWriter sw = new StreamWriter(fs);
                sw.Write(md.BookContent);
                sw.Close();
                fs.Close();

                (new HtmlConverter()).ConvertToDoc("C://tmp//tmp.html", FileToSave);
            }
            catch { throw; } 
        }

        public void ConvertToDocX(string SourcePath, string FileToSave)
        {
            if (File.Exists(FileToSave))
            {
                File.Delete(FileToSave);
            }
            try
            {
                if (!System.IO.Directory.Exists("C://tmp"))
                    System.IO.Directory.CreateDirectory("C://tmp");
                else
                {
                    foreach (FileInfo f in (new DirectoryInfo("C://tmp")).GetFiles())
                        f.Delete();
                    foreach (DirectoryInfo dir in (new DirectoryInfo("C://tmp")).GetDirectories())
                    {
                        dir.Delete(true);
                    }
                }

                MobiDoc md = new MobiDoc();
                md.loadContent(SourcePath);

                System.IO.FileStream fs = new System.IO.FileStream("C://tmp//tmp.html", System.IO.FileMode.Create, System.IO.FileAccess.Write);
                StreamWriter sw = new StreamWriter(fs);
                sw.Write(md.BookContent);
                sw.Close();
                fs.Close();

                (new HtmlConverter()).ConvertToDocX("C://tmp//tmp.html", FileToSave);
            }
            catch { throw; }
        }

        public void ConvertToPdf(string SourcePath, string FileToSave)
        {
            if (File.Exists(FileToSave))
            {
                File.Delete(FileToSave);
            }
            try
            {
                if (!System.IO.Directory.Exists("C://tmp"))
                    System.IO.Directory.CreateDirectory("C://tmp");
                else
                {
                    foreach (FileInfo f in (new DirectoryInfo("C://tmp")).GetFiles())
                        f.Delete();
                    foreach (DirectoryInfo dir in (new DirectoryInfo("C://tmp")).GetDirectories())
                    {
                        dir.Delete(true);
                    }
                }

                MobiDoc md = new MobiDoc();
                md.loadContent(SourcePath);

                System.IO.FileStream fs = new System.IO.FileStream("C://tmp//tmp.html", System.IO.FileMode.Create, System.IO.FileAccess.Write);
                StreamWriter sw = new StreamWriter(fs);
                sw.Write(md.BookContent);
                sw.Close();
                fs.Close();

                (new HtmlConverter()).ConvertToPdf("C://tmp//tmp.html", FileToSave);
            }
            catch { throw; }
        }

        public void ConvertToHtml(string SourcePath, string FileToSave)
        {
            if (File.Exists(FileToSave))
            {
                File.Delete(FileToSave);
            }
            try
            {
                if (!System.IO.Directory.Exists("C://tmp"))
                    System.IO.Directory.CreateDirectory("C://tmp");
                else
                {
                    foreach (FileInfo f in (new DirectoryInfo("C://tmp")).GetFiles())
                        f.Delete();
                    foreach (DirectoryInfo dir in (new DirectoryInfo("C://tmp")).GetDirectories())
                    {
                        dir.Delete(true);
                    }
                }

                MobiDoc md = new MobiDoc();
                md.loadContent(SourcePath);

                System.IO.FileStream fs = new System.IO.FileStream(FileToSave, System.IO.FileMode.Create, System.IO.FileAccess.Write);
                StreamWriter sw = new StreamWriter(fs);
                sw.Write(md.BookContent);
                sw.Close();
                fs.Close();
            }
            catch { throw; }
        }

    }
}
